package com.freewater.springboot.demo.aop.api.advised;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.DebugInterceptor;

public class Main {
    public static void main(String[] args) {
        SomeService someService = new SomeService();

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvice(new SimpleBeforeAdvice());
        proxyFactory.setTarget(someService);
        //proxyFactory.setFrozen(true);

        SomeService proxy = (SomeService) proxyFactory.getProxy();
        proxy.hello("freewater");
        proxy.sayHi();

        Advised advised = (Advised) proxy;
        SimpleAfterAdvice afterAdvice = new SimpleAfterAdvice();
        advised.addAdvice(afterAdvice);

        proxy.hello("freewater");
        proxy.sayHi();

        System.out.println(advised.getAdvisorCount());
        for (Advisor advisor : advised.getAdvisors()) {
            System.out.println("advisor: " + advisor);
        }

        advised.removeAdvice(afterAdvice);
        System.out.println(advised.getAdvisorCount());
        for (Advisor advisor : advised.getAdvisors()) {
            System.out.println("advisor: " + advisor);
        }

        System.out.println(advised.isFrozen());
        advised.addAdvice(new DebugInterceptor(true));
        proxy.hello("freewater");
        proxy.sayHi();
    }
}
